import { ResponseCode } from "./constants";

class Response {
    private readonly response = { success: true, code: ResponseCode.ok }

    public okMessage(message = '成功') {
        return { ...this.response, message };
    }

    public okData<T>(data: T) {
        return { ...this.response, data };
    }

    public error(message: string) {
        return { ...this.response, success: false, message: message, code: ResponseCode.error };
    }

    public errorDetail(detail: string = '') {
        return { ...this.error(`数据格式不正确`), code: ResponseCode.dataError, detail };
    }

    public errorDataDecode(detail: string = '') {
        return { ...this.error(`数据解密错误`), code: ResponseCode.dataDecodeError, detail };
    }

    public errorDataSign(detail: string = '') {
        return { ...this.error(`数据签名错误`), code: ResponseCode.dataSignError, detail };
    }

    public errorNotFound() {
        return { ...this.error(`404, Not Found`), code: ResponseCode.notFoundError };
    }

    public errorForbidden() {
        return { ...this.error(`未授权`), code: ResponseCode.forbiddenError };
    }

    public errorUnauthorized() {
        return { ...this.error(`没有Basic授权验证`), code: ResponseCode.unauthorizedError };
    }

    public errorServer(detail: string = '') {
        return { ...this.error(`服务端错误`), code: ResponseCode.serverError, detail };
    }
}

export default new Response();